#include "d3dUtil.h"
#include "D3DApp.h"
#include "GfxStats.h"
#include <tchar.h>

GfxStats::GfxStats(void)
	:mFont(0),mFPS(0.0f),mMilliSecPerFrame(0.0f),mNumTris(0),mNumVertices(0)
{
	D3DXFONT_DESC	fontDesc;
	fontDesc.Height			= 18;
	fontDesc.Width			= 0;
	fontDesc.Weight			= 0;
	fontDesc.MipLevels		= 1;
	fontDesc.Italic			= false;
	fontDesc.CharSet		= DEFAULT_CHARSET;
	fontDesc.OutputPrecision= OUT_DEFAULT_PRECIS;
	fontDesc.Quality		= DEFAULT_QUALITY;
	fontDesc.PitchAndFamily = DEFAULT_PITCH|FF_DONTCARE;
	_tcscpy(fontDesc.FaceName,_T("Times New Roman"));

	HR(D3DXCreateFontIndirect(gd3dDevice,&fontDesc,&mFont));
}


GfxStats::~GfxStats(void)
{
	ReleaseCOM(mFont);
}


void GfxStats::onLostDevice()
{
	HR(mFont->OnLostDevice());
}

void GfxStats::onResetDevice()
{
	HR(mFont->OnResetDevice());
}

void GfxStats::addVertices(DWORD n)
{
	mNumVertices += n;
}

void GfxStats::subVertices(DWORD n)
{
	mNumVertices -= n;
}

void GfxStats::addTriangles(DWORD n)
{
	mNumTris += n;
}

void GfxStats::subTriangles(DWORD n)
{
	mNumTris -= n;
}

void GfxStats::setTriCount(DWORD n)
{
	mNumTris = n;
}

void GfxStats::setVertexCount(DWORD n)
{
	mNumVertices = n;
}

void GfxStats::update(float dt)
{
	//Make static so that their values persist accross function calls;
	static float numFrames		= 0.0f;
	static float timeElapsed	= 0.0f;

	//Increment the frame count.
	numFrames	+= 1.0f;

	//Accumulate how much time has passed
	timeElapsed	+= dt;

	//Has one second passed? -- we compute the frame statistics once
	//Per second. Node that the time between frames can vary so 
	//these stats are average over a second
	if (timeElapsed>= 1.0f)
	{
		//Frames Per Second = numFrames / timeElapsed.
		//but timeElapsed approx . equals 1.0 , so 
		//frame per second = numFrames

		mFPS = numFrames;

		//Average time , in miliseconds , it took to render a single frame
		mMilliSecPerFrame = 1000.0f/mFPS;

		//Resent time counter and frame count to prepare for computing
		//the average stats over the next second.
		timeElapsed = 0.0f;
		numFrames	= 0.0f;
	}
}

void GfxStats::display()
{
	//Make static so memory is not allocated every frame;
	static char buffer[256];

	sprintf(buffer, "Frames Per Second = %.2f\n"
		"Milliseconds Per Frame = %.4f\n"
		"Triangle Count = %d\n"
		"Vertex Count = %d", mFPS, mMilliSecPerFrame, mNumTris, mNumVertices);

	RECT R = {5,5,0,0};

	HR(mFont->DrawText(0,buffer,-1,&R,DT_NOCLIP,D3DCOLOR_XRGB(0,0,0)));
}